<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Shicai extends CF_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->load->model('Shicai_mdl');
		$this->load->helper('text');
		$this->uid = $this->user_login->is_logged_in() ? $this->user_login->get_user_id() : 0;
	}

	public function index()
	{
		$data['category'] = $this->Shicai_mdl->get_shicai_category(0)->result_array();	//获取食材顶级分类 参数"0"
		$this->load->view('shicai',$data);
	}

	/**
	 * 食材内容页
	 * @param  string $id [description]
	 * @return [type]     [description]
	 */
	public function ingredients($id='',$info='')
	{
		$this->load->model('caipu_mdl');
		
		/** 如为空，则跳转 */
		if (empty($id) || !is_numeric($id)) redirect(site_url());

		$shicai_info = $this->Shicai_mdl->get_shicai_by_id($id);
		/** 不存在? */
		if (!$shicai_info) show_404();

		/* 分页配置 */
		$page = max(1, intval($info));
		$limit = 15; //每页显示数
		$offset = ($page - 1) * $limit;

		/** 内容显示格式化 */
		$data['shicai_info'] = $this->_prepare_shicai_info($shicai_info);

		$caipu_array = $this->caipu_mdl->get_caipu_by_shicai($shicai_info['title'], $offset, $limit)->result_array();

		$rows_array = array();
		foreach ($caipu_array as $value) 
		{
			$value['coverImage'] = server_img($value['coverImage']);
			$rows_array[] = $value;
		}
		$data['shicai_like_caipu'] = $rows_array;

		$data_count = 10000;


		/* 分页配置 */
		$this->load->library('pagination');
		$config['base_url'] = site_url('ingredients/'.$id.'/');
		$config['total_rows'] = $data_count;
		$config['per_page'] = $limit; 
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config); 
		$this->load->view('shicai_ingredients',$data);

	}
	
	/**
	 * 食材列表页
	 * @param  string $page [description]
	 * @return [type]       [description]
	 */
	public function cate($classid='',$page='')
	{
		$page = max(1, intval($page));
		$limit = 15; //每页显示数
		$offset = ($page - 1) * $limit;

		$data_count = $this->Shicai_mdl->get_shicai_list_by_pid($classid)->num_rows();			//获取食材列表总数

		/* 分页配置 */
		$this->load->library('pagination');
		$config['base_url'] = site_url('shicai/cate/'.$classid.'/');					//URL
		$config['total_rows'] = $data_count;											//总记录数
		$config['per_page'] = $limit;													//每页记录条数
		$config['uri_segment'] = 4;														//当前页URL段
		$this->pagination->initialize($config); 

		$data['category'] = $this->Shicai_mdl->get_shicai_category(0)->result_array();	//获取食材顶级分类 参数"0"
		$data['cate_by_classid'] = $this->Shicai_mdl->get_shicai_category($classid)->result_array();
		$data['cate_by_name'] = $this->Shicai_mdl->get_shicai_category_name($classid);	//获取当前食材分类名称
		
		$data['shicai'] = $this->Shicai_mdl->get_shicai_list_by_pid($classid, $offset, $limit)->result_array();	//获取食材列表

		$this->load->view('shicai_cate',$data);
	}
	/**
	 * 食材列表页
	 * @param  string $page [description]
	 * @return [type]       [description]
	 */
	public function subcate($classid='',$page='')
	{
		$page = max(1, intval($page));
		$limit = 15; //每页显示数
		$offset = ($page - 1) * $limit;

		$data_count = $this->Shicai_mdl->get_shicai_list($classid)->num_rows();			//获取食材列表总数

		/* 分页配置 */
		$this->load->library('pagination');
		$config['base_url'] = site_url('shicai/subcate/'.$classid.'/');					//URL
		$config['total_rows'] = $data_count;											//总记录数
		$config['per_page'] = $limit;													//每页记录条数
		$config['uri_segment'] = 4;														//当前页URL段
		$this->pagination->initialize($config); 

		$data['category'] = $this->Shicai_mdl->get_shicai_category(0)->result_array();	//获取食材顶级分类 参数"0"
		$data['cate_by_name'] = $this->Shicai_mdl->get_shicai_category_name($classid);	//获取当前食材分类名称
		
		$data['shicai'] = $this->Shicai_mdl->get_shicai_list($classid, $offset, $limit)->result_array();	//获取食材列表

		$this->load->view('shicai_subcate',$data);
	}

	private function _prepare_shicai_info($shicai_info)
	{
		$id = $shicai_info['id'];
		
		/** 判断是否喜欢 */
		$this->load->model('user/user_like','ulike');
		$shicai_info['is_like'] = (0 == $this->ulike->get_shicai_like($id, $this->uid)) ? FALSE : TRUE;
		$shicai_info['cate_name'] = $this->Shicai_mdl->get_shicai_category_name($shicai_info['classid']);	//获取当前食材分类名称

		$shicai_xiangke_query = $this->Shicai_mdl->get_shicai_xiangke_by_title($shicai_info['title']);
		$shicai_xiangke_query = $shicai_xiangke_query ? $shicai_xiangke_query : 0;

		$shicai_info['xiangsheng_array'] = array_filter(explode('|', $shicai_xiangke_query['xiangsheng']));
		$shicai_info['xiangke_array'] = array_filter(explode('|', $shicai_xiangke_query['xiangke']));
		
		return $shicai_info;
	}
}

/* End of file shicai.php */
/* Location: ./application/controllers/shicai.php */